//
//  CategoryViewController.m
//  freeLance
//
//  Created by Igor Karpenko on 3/27/13.
//  Copyright (c) 2013 Mozi Development. All rights reserved.
//

#import "CategoryViewController.h"
#import "CoreDataRoutines.h"
#import "CategoryViewCell.h"

#import "CollectionViewController.h"

@interface CategoryViewController ()

@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, assign) CGPoint selectedCollectionPosition;
@property (nonatomic, strong) CDCollections *selectedColletion;

@end

@implementation CategoryViewController


#pragma mark - Instance initialization

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (!self) {
        return nil;
    }

	return self;
}


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"CategoryViewControllerToCollectionViewController"]) {
        CollectionViewController *desinationViewController = (CollectionViewController *)[segue destinationViewController];
        desinationViewController.collection = self.selectedColletion;
        desinationViewController.point = self.selectedCollectionPosition;
    }
}


#pragma mark - Interface methods

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadColletions:)
                                                 name:ProductManagerFinishLoadingAndSynchronization
                                               object:nil];
}

-(void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:ProductManagerFinishLoadingAndSynchronization
                                                  object:nil];
}

-(void) viewDidLoad {
    [super viewDidLoad];
	
	UINib *cellNib = [UINib nibWithNibName:@"CategoryViewCell" bundle:nil];
	[_collectionView registerNib:cellNib
	  forCellWithReuseIdentifier:categoryCellIdentifier];


	UINib *headerNib = [UINib nibWithNibName:@"CollectionHeaderView" bundle:nil];
	[_collectionView registerNib:headerNib
	  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
			 withReuseIdentifier:collectionHeaderIdentifier];

	_dataSource = [[CategoryDataSource alloc] init];
	_collectionView.dataSource = _dataSource;
    
    NSArray *categories = [[CoreDataRoutines sharedInstance] categories];
    NSMutableArray *categoriesName = [NSMutableArray arrayWithCapacity:categories.count];
    
    for (CDCategories *category in categories) {
        [categoriesName addObject:category.name];
    }
	
	_segmentedControl = [[UISegmentedControl alloc] initWithItems:categoriesName];
	self.segmentedControl.frame = CGRectMake(0.0f, 0.0f, 300.0f, 30.0f);
	[self.segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
	self.navigationItem.titleView = self.segmentedControl;
	[self.segmentedControl addTarget:self
						 action:@selector(segmentedControlChanged:)
			   forControlEvents:UIControlEventValueChanged];
	
    self.segmentedControl.selectedSegmentIndex = 0;
	[self segmentedControlChanged:self.segmentedControl];
}


#pragma mark - Action methods

-(void) segmentedControlChanged:(UISegmentedControl *)sender {
	[_dataSource fetchItemsWithCategoryId:[NSNumber numberWithInt:sender.selectedSegmentIndex + 1]];
	[_collectionView reloadData];
}


#pragma mark - Notification observers

-(void) reloadColletions:(NSNotification *) notification {
    [self viewDidLoad];
}


#pragma mark - Delegated methods

-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    self.selectedCollectionPosition = [self.view convertPoint:cell.frame.origin fromView:_collectionView];
    self.selectedColletion = _dataSource.items[indexPath.row];
	
    [self performSegueWithIdentifier:@"CategoryViewControllerToCollectionViewController" sender:self];
}

@end
